import {
  ProForm,
  ProFormText,
} from '@ant-design/pro-form';
import { message } from 'antd';
import {editPassword} from "@/pages/User/Account/settings/service";
import {history} from '@umijs/max';

export default () => {
  return (
    <ProForm<{
      name: string;
      company?: string;
      useMode?: string;
    }>
      onFinish={async (values) => {
        const res = await editPassword({...values});
        if(res && res.status === "done"){
          message.success("密码修改成功，请重新登录！")
          // 清除本地token
          localStorage.clear()
          history.push('/login')
          return true;
        }else {
          message.error(res.message);
          return false;
        }
      }}
    >
      <ProFormText.Password
        width="xl"
        name="oldPassword"
        label="验证旧密码"
        tooltip={"请输入当前使用的密码。"}
        rules={[
          {required: true, message: "旧密码不可为空！"}
        ]}
      />
      <ProFormText.Password
        width="xl"
        name="newPassword1"
        label="新密码"
        rules={[
          {required: true, message: "新密码不可为空！"},
          {min:6, message: "密码至少需要6个字符！"}
        ]}
      />
      <ProFormText.Password
        width="xl"
        name="newPassword2"
        label="确认密码"
        rules={[
          {required: true, message: "新密码不可为空！"},
          {min:6, message: "密码至少需要6个字符！"}
        ]}
      />
    </ProForm>
  );
};
